T1 P0
T0 P0

M291 S5 J1 F250 L190 H450 R"Set Left Tool Temperature" P"Set temperature of the filament that was last loaded in the Left Tool"
var ll = input

M291 S5 F250 L190 H450 R"Set Right Tool Temperature" P"Set temperature of the filament that was last loaded in the Right Tool"
var rr = input

G10 P0 R{var.ll} S{var.ll}
G10 P1 R{var.rr} S{var.rr}
G10 P2 R{var.ll,var.rr} S{var.ll,var.rr}
G10 P3 R{var.ll,var.rr} S{var.ll,var.rr}


if !move.axes[0].homed || !move.axes[1].homed || !move.axes[2].homed || !move.axes[3].homed
  M98 P"0:/sys/homeall.g" S1

M116 P0 S10
M116 P1 S10

T3
M83                 ; relative extruder moves
G1 E-20 F{60}*{50}  ; retract filament
T0

G1 F18000 X-50 U50 Y-150 Z200  ; Move tools to the cleaning position

M291 R"Clean both Nozzles well with a metal brush" P"Otherwise you risk damaging your Machine. Click OK when ready to proceed" S3

M98 P"0:/sys/homeall.g" S1

G90 ; Absolute
G1 Y0 X-999 U999 Z200 F18000
M291 R"Connect the probe and place it in the center of the print bed" P"Click OK when ready to proceed" S3
; Park second tool
T0
G90 ; Absolute
G1 X30 Y30 Z23                 ; Move main tool to position

M291 R"Slide the probe under the nozzle" P"Click OK when ready to proceed" S3
M558 K0 P5 C"!duex.e6stop"     ; Reassign the probe
G4 P500
M42 P4 S1                      ; Turn on relay

G91 ; Relative

G38.2 K0 Z-5 ; Probe Z
G1 Z2

; ===== Probe with the Right tool =====

;Move to the 1st probing point
G1 Y0 X-30        ;Move to position
G1 Z-2.5
G38.2 K0 X50 ; Probe LX
G4 P260
var spLX = move.axes[0].userPosition

;Move to the 2nd probing point
G1 X-2
G1 Y20
G1 X30
G1 Y-20
G38.2 K0 X-50 ; Probe LX
G4 P260
var fpLX = move.axes[0].userPosition

;Move to the 3rd probing point
G1 X5
G1 Y20
var diffLX = var.fpLX - var.spLX
var cpLX = var.fpLX - var.diffLX / 2
G90 ; Absolute
G1 X{var.cpLX}
G91 ; Back to relative
G38.2 K0 Y-50 ; Probe LY
G4 P260
var fpLY = move.axes[1].userPosition

;Move to the 4th probing point
G1 Y5
G1 X-25
G1 Y-40
G90 ; Absolute
G1 X{var.cpLX}
G91 ; Back to relative
G38.2 K0 Y50 ; Probe LY
G4 P260
var spLY = move.axes[1].userPosition

var diffLY = var.fpLY - var.spLY
var cpLY = var.fpLY - var.diffLY / 2

G1 Y-2
G1 X{move.axes[0].min}


; ===== Probe with the Left tool =====

;Move to the 1st probing point
G90 ; Absolute
G1 U{var.cpLX + 20} Y{var.cpLY}
G91 ; Back to relative
G38.2 K0 U-50 ; Probe RX (first)
G4 P260
var fpRX = move.axes[3].userPosition

;Move to the 2nd probing point
G1 U2
G1 Y15
G1 U-30
G1 Y-15
G38.2 K0 U50 ; Probe RX
G4 P260
var spRX = move.axes[3].userPosition

var diffRX = var.fpRX - var.spRX
var cpRX = var.fpRX - var.diffRX / 2

;Move to the 3rd probing point
G1 U-2
G1 Y15
G90 ; Absolute
G1 U{var.cpRX}
G91 ; Back to relative
G38.2 K0 Y-50 ; Probe RY
G4 P260
var fpRY = move.axes[1].userPosition

;Move to the 4th probing point
G1 Y2
G1 U15
G1 Y-30
G90 ; Absolute
G1 U{var.cpRX}
G91 ; Back to relative
G38.2 K0 Y50 ; Probe RY (second)
G4 P260
var spRY = move.axes[1].userPosition
var diffRY = var.fpRY - var.spRY
var cpRY = var.fpRY - var.diffRY / 2

M42 P4 S0                      ; Power off servo relay
M558 K0 P5 C"duex.e6stop"      ; Reassign probe back
M204 T5000                     ; set accelerations

set global.uoffset = var.cpLX - var.cpRX
set global.yoffset = var.cpLY - var.cpRY

; Generate uoffset.g (persistent uoffset var)
echo >"0:/user/uoffset.g" "if exists(global.uoffset)"
echo >>"0:/user/uoffset.g" "  set global.uoffset = "^{global.uoffset}
echo >>"0:/user/uoffset.g" "else"
echo >>"0:/user/uoffset.g" "  global uoffset = "^{global.uoffset}

; Generate yoffset.g (persistent yoffset var)
echo >"0:/user/yoffset.g" "if exists(global.yoffset)"
echo >>"0:/user/yoffset.g" "  set global.yoffset = "^{global.yoffset}
echo >>"0:/user/yoffset.g" "else"
echo >>"0:/user/yoffset.g" "  global yoffset = "^{global.yoffset}

; Generate tooloffset.g
echo >"0:/user/tooloffset.g" "; Set tool offsets"
echo >>"0:/user/tooloffset.g" "G10 P1 U"^{global.uoffset}^" Y"^{global.yoffset}

; Apply changes
G10 P1 U{global.uoffset} Y{global.yoffset}
echo "Current offsets: U"^{global.uoffset}^" Y"^{global.yoffset}

; Park second tool
G1 Z5
G90
G1 X{move.axes[0].min} U{move.axes[3].max} F18000 Z200 F18000

G10 P0 R0 S0
G10 P1 R0 S0
G10 P2 R0 S0
G10 P3 R0 S0

M291 R"Finished successfully, Remove the probe!" P" " S2